<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="周次" prop="weekTime">
            <el-date-picker
              v-model="formItem.weekTime"
              type="week"
              format="yyyy 第 WW 周"
              placeholder="选择周次"
              value-format="yyyy-MM-dd HH:mm:ss"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="unitCode">
            <el-select v-model="formItem.unitCode" filterable placeholder="请选择单位" style="width:100%" :disabled="unableSelectUnit" @change="unitChange">
              <el-option v-for="item in availableInstitutions" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调研准备(%)" prop="survey">
            <el-input-number v-model="formItem.survey" :min="0" :max="100" :precision="0" label="调研准备" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上门调研(%)" prop="dropIn">
            <el-input-number v-model="formItem.dropIn" :min="0" :max="100" :precision="0" label="上门调研" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调研资料整理(%)" prop="material">
            <el-input-number v-model="formItem.material" :min="0" :max="100" :precision="0" label="调研资料整理" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资产盘点(%)" prop="assetInventory">
            <el-input-number v-model="formItem.assetInventory" :min="0" :max="100" :precision="0" label="资产盘点" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资产录入(%)" prop="assetEntry">
            <el-input-number v-model="formItem.assetEntry" :min="0" :max="100" :precision="0" label="资产录入" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="资产入库(%)" prop="assetWarehousing">
            <el-input-number v-model="formItem.assetWarehousing" :min="0" :max="100" :precision="0" label="资产入库" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="方案定制(%)" prop="schemeCustomization">
            <el-input-number v-model="formItem.schemeCustomization" :min="0" :max="100" :precision="0" label="方案定制" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始化健康检查(%)" prop="healthCheckup">
            <el-input-number v-model="formItem.healthCheckup" :min="0" :max="100" :precision="0" label="初始化健康检查" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单项完成进度(%)" prop="schedule">
            <el-input-number v-model="formItem.schedule" :min="0" :max="100" :precision="0" label="单项完成进度" style="width:100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="完工时间" prop="closingTime">
            <el-date-picker v-model="formItem.closingTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择完工时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left" />
      <el-row>
        <el-col :span="24">
          <el-form-item label="本周工作完成情况">
            <el-input v-model="formItem.weekDescription" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="总结描述" prop="summaryDescription">
            <el-input v-model="formItem.summaryDescription" placeholder="请输入总结描述" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="下周工作计划" prop="workPlan">
            <el-input v-model="formItem.workPlan" placeholder="请输入下周工作计划" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="问题记录及解决方案" prop="problemLog">
            <el-input v-model="formItem.problemLog" placeholder="请输入问题记录及解决方案" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="formItem.remarks" placeholder="请输入" type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" resize="none" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { create, update, detail, gatLatest } from '@neu/dcom-ui/api/transferWeekly'
import { formatDate, getFirstDayOfWeekOfYear } from '@neu/biz-core/utils/utils'
import { mapGetters } from 'vuex'

// 定义一个表单原始项
const formItem = {
  /** 单位名称 */
  unitName: '',
  /** 单位编码 */
  unitCode: '',
  /** 本周工作情况 */
  weekDescription: '',
  /** 调研准备 */
  survey: 0,
  /** 初始化健康检查 */
  healthCheckup: 0,
  /** 上门调研 */
  dropIn: 0,
  /** 单项完成进度 */
  schedule: 0,
  /** 调研资料整理 */
  material: 0,
  /** 完工时间 */
  closingTime: '',
  /** 资产盘点 */
  assetInventory: 0,
  /** 资产入库 */
  assetWarehousing: 0,
  /** 资产录入 */
  assetEntry: 0,
  /** 方案定制 */
  schemeCustomization: 0,
  /** 总结描述 */
  summaryDescription: '',
  /** 下周工作计划 */
  workPlan: '',
  /** 问题记录及解决方案 */
  problemLog: '',
  /** 备注 */
  remarks: '',
  /** 周次 */
  weekTime: formatDate(getFirstDayOfWeekOfYear(new Date()))
}

const formRules = {
  unitCode: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
  weekTime: [{ required: true, message: '周次不能为空', trigger: 'blur' }]
}

export default {
  name: 'TransferWeeklyEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      /** 行数 */
      rows: 2,
      // 所有单位
      availableInstitutions: [],
      // 表单验证规则
      rules: { ...formRules },
      availableUsers: []
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ]),
    // 是否可选择厅局
    unableSelectUnit() {
      return this.roles.some(r => r === 'worker')
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler: 'fetch',
      immediate: true
    }
  },
  mounted() {
    this.fetch()
    this.availableInstitutions = this.$store.getters['dcom/projectUserInfo'].institutions
    this.formItem.unitCode = this.$store.getters['dcom/projectUserInfo'].defaultInstituteCode
    if (this.formItem.unitCode) {
      this.gatLatestReport()
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          const data = response.data
          this.formItem = {
            ...data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            // 公司名称
            const institutions = this.availableInstitutions.find(e => this.formItem.unitCode === e.code)
            this.formItem.unitName = institutions ? institutions.name : ''
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    },
    unitChange(val) {
      if (val) {
        this.gatLatestReport()
      }
    },
    gatLatestReport() {
      gatLatest(this.formItem.unitCode).then(response => {
        if (response.data) {
          this.formItem.survey = response.data.survey
          this.formItem.healthCheckup = response.data.healthCheckup
          this.formItem.dropIn = response.data.dropIn
          this.formItem.schedule = response.data.schedule
          this.formItem.material = response.data.material
          this.formItem.assetInventory = response.data.assetInventory
          this.formItem.assetWarehousing = response.data.assetWarehousing
          this.formItem.assetEntry = response.data.assetEntry
          this.formItem.schemeCustomization = response.data.schemeCustomization
        } else {
          this.formItem.survey = 0
          this.formItem.healthCheckup = 0
          this.formItem.dropIn = 0
          this.formItem.schedule = 0
          this.formItem.material = 0
          this.formItem.assetInventory = 0
          this.formItem.assetWarehousing = 0
          this.formItem.assetEntry = 0
          this.formItem.schemeCustomization = 0
        }
      })
    }
  }
}
</script>
